package com.example.appointment.common.http;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.appointment.R;
import com.example.appointment.common.Utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class RequestUtil {

	private List<HttpUtil> connectedHttp = new ArrayList<HttpUtil>();

	private Context mContext;


	private ProgressDialog progressDialog;
	private HttpCallbackListener httpCallbackListener;

	public RequestUtil(Context context) {
		mContext = context;
		try {
			progressDialog = new ProgressDialog(mContext);
		} catch (Exception e) {
		}
	}

	public void disConnectAll() {
		dismissProgressDialog();
		for (HttpUtil httpUtil : connectedHttp) {
			httpUtil.disConnect();
		}
	}

	public HttpUtil getHttpUtil() {
		HttpUtil httpUtil = new HttpUtil();
		connectedHttp.add(httpUtil);
		return httpUtil;
	}

//	class Builder {
//		private Context builderContext;
//		private String requestUrl;
//		private Map<String, Object> requestParams;
//		private HttpUtil.RequestMethod requestMethod;
//		private HttpUtil.HttpCallbackListener callbackListener;
//
//		public Builder(Context context) {
//			builderContext = context;
//		}
//
//		public Builder url(String url) {
//			this.requestUrl = url;
//			return this;
//		}
//
//		public Builder params(Map<String, Object> params) {
//			requestParams = params;
//			return this;
//		}
//
//		public  Builder requestMethod(HttpUtil.RequestMethod method){
//			requestMethod = method;
//			return this;
//		}
//
//		public  Builder requestMethod(HttpUtil.HttpCallbackListener listener){
//			callbackListener = listener;
//			return this;
//		}
//		public RequestUtil build(){
//			RequestUtil requestUtil = new RequestUtil(builderContext);
//
//
//
//
//		}
//
//	}


	public void requestWithProgress(String url, HttpUtil.RequestMethod method, Map<String, Object> params,
	                                final HttpCallbackListener listener) {
		showProgressDialog();
		sendRequest(url, method, params, listener);
	}


	public void requestWithoutProgress(String url, HttpUtil.RequestMethod method, Map<String, Object> params,
	                                   final HttpCallbackListener listener) {
		sendRequest(url, method, params, listener);
	}


	private void sendRequest(String url, HttpUtil.RequestMethod method, Map<String, Object> params, HttpCallbackListener listener) {
		HttpUtil httpUtil = new HttpUtil();
		switch (method) {
			case DELETE:
				httpUtil.delete(url, params, HttpUtil.RequestBodyType.FORM, new TopHttpCallbackListener(httpUtil, listener));
				break;
			case GET:
				httpUtil.get(url, params, new TopHttpCallbackListener(httpUtil, listener));
				break;
			case POST:
				httpUtil.post(url, params, HttpUtil.RequestBodyType.FORM, new TopHttpCallbackListener(httpUtil, listener));
				break;
			case PUT:
				httpUtil.put(url, params, HttpUtil.RequestBodyType.FORM, new TopHttpCallbackListener(httpUtil, listener));
				break;
		}
		connectedHttp.add(httpUtil);
	}


	class TopHttpCallbackListener extends HttpCallbackListener {
		HttpUtil httpUtil;
		HttpCallbackListener httpCallbackListener;

		public TopHttpCallbackListener(HttpUtil httpUtil, HttpCallbackListener customListener) {
			this.httpUtil = httpUtil;
			this.httpCallbackListener = customListener;
		}

		@Override
		public void onRequestSuccess(String result, Object requestData) {
			dismissProgressDialog();
			connectedHttp.remove(httpUtil);
			if (httpCallbackListener != null) {
				httpCallbackListener.onRequestSuccess(result, requestData);
			}
		}

		@Override
		public boolean onRequestFailed(int resultCode) {
			dismissProgressDialog();
			connectedHttp.remove(httpUtil);
			if (httpCallbackListener != null) {
				if (!httpCallbackListener.onRequestFailed(resultCode)) {
					//TODO
				}
			} else {
				//TODO
			}
			return false;
		}

		@Override
		public void onConnectionFailed() {
			dismissProgressDialog();
			connectedHttp.remove(httpUtil);
			Utils.showShortToast(mContext, mContext.getString(R.string.net_error));
			if (httpCallbackListener != null) {
				httpCallbackListener.onConnectionFailed();
			}
		}

		@Override
		public void onServerError(String result, int statusCode) {
			dismissProgressDialog();
			connectedHttp.remove(httpUtil);
			if (statusCode == 401) {
				PersistentCookieStore persistentCookieStore = new PersistentCookieStore(mContext.getApplicationContext());
				persistentCookieStore.removeAll();
				Intent intent = new Intent("login");
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				mContext.startActivity(intent);
				if(mContext instanceof Activity){
					((Activity) mContext).finish();
				}
				return;
			}
			//String msg = null;

			if (TextUtils.isEmpty(result)) {
				Spanned spanned = Html.fromHtml(result);
				result = String.valueOf(spanned);
			}
			if (httpCallbackListener != null) {
				httpCallbackListener.onServerError(result, statusCode);
			}
		}
	}

	public void showProgressDialog() {
		if (progressDialog != null && !progressDialog.isShowing()) {
			progressDialog.show();
		}
	}


	public void dismissProgressDialog() {
		if (progressDialog != null && progressDialog.isShowing()) {
			progressDialog.dismiss();
		}
	}

}
